﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace Travianer.Common
{
    public class Configuration
    {
        public static string ServerLocation
        {
            get
            {
                return ConfigurationManager.AppSettings["ServerLocation"];
            }
            set
            {
                ConfigurationHelper.GetInstance().UpdateConfig("ServerLocation", value);
            }
        }
        public static string UserName
        {
            get
            {
                return ConfigurationManager.AppSettings["UserName"];
            }
            set
            {
                ConfigurationHelper.GetInstance().UpdateConfig("UserName", value);
            }
        }
        public static string Password
        {
            get
            {
                return ConfigurationManager.AppSettings["Password"];
            }
            set
            {
                ConfigurationHelper.GetInstance().UpdateConfig("Password", value);
            }
        }
        public static Encoding Encoding
        {
            get
            {
                return Encoding.GetEncoding(ConfigurationManager.AppSettings["Encoding"]);
            }
        }
        public static string LoginString
        {
            get
            {
                return ConfigurationManager.AppSettings["LoginString"];
            }
        }
        public static string Wood
        {
            get
            {
                return ConfigurationManager.AppSettings["Wood"];
            }
        }
        public static string Clay
        {
            get
            {
                return ConfigurationManager.AppSettings["Clay"];
            }
        }
        public static string Iron
        {
            get
            {
                return ConfigurationManager.AppSettings["Iron"];
            }
        }
        public static string Crop
        {
            get
            {
                return ConfigurationManager.AppSettings["Crop"];
            }
        }
        public static string AssemblyPath
        {
            get
            {
                return ConfigurationManager.AppSettings["AssemblyPath"];
            }
        }
        public static string AssemblyPathPrefix
        {
            get
            {
                return ConfigurationManager.AppSettings["AssemblyPathPrefix"];
            }
        }
    }

    internal sealed class ConfigurationHelper
    {
        private static readonly ConfigurationHelper _Instance;
        static ConfigurationHelper()
        {
            _Instance = new ConfigurationHelper();
        }

        private ConfigurationHelper() { }

        public static ConfigurationHelper GetInstance()
        {
            return _Instance;
        }
        /// <summary>
        /// 更新配置文件信息
        /// </summary>
        /// <param name="key">配置文件字段名称</param>
        /// <param name="Xvalue">值</param>
        public void UpdateConfig(string key, string Xvalue)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
            XmlNode node = doc.SelectSingleNode(@"//add[@key='" + key + "']");
            XmlElement ele = (XmlElement)node;
            ele.SetAttribute("value", Xvalue);
            doc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
        }
    }
}
